using System;
using System.IO;
using System.Reflection;
using SpecMaker.Core.Interfaces;

namespace SpecMaker.Core
{
    public class HtmlReporter :IReportFactory
    {
        private readonly IIOWriter _iowriter;

        public HtmlReporter(IIOWriter iowriter)
        {
            _iowriter = iowriter;
        }

        public void FromSpecContainers(SpecResult[] specs)
        {
          
            _iowriter.Open(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)+"\\"+"spec-report.html");
            _iowriter.WriteLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            _iowriter.WriteLine("<html>");
            _iowriter.WriteLine("<head>");
            _iowriter.WriteLine(@"<style>
                                div {
                                border: .2em solid #900;
                                }
                                </style>");
            _iowriter.WriteLine("</head>");
            _iowriter.WriteLine("<body>");
            foreach (var spec in specs)
            {
                _iowriter.WriteLine("<div>");
                _iowriter.WriteLine(String.Format("<span>Item under specification: {0}</span><br/>", spec.SpecObject));
                _iowriter.WriteLine(String.Format("<span>Situation: {0}</span>", spec.Context));
                _iowriter.WriteLine("<table>");
                _iowriter.WriteLine("<thead><tr><th>Rule</th><th>Result</th></tr></thead>");
                _iowriter.WriteLine("<tbody");
                foreach (var result in spec.Results)
                {
                    _iowriter.WriteLine(String.Format("<tr><td>It Should {0}</td><td>{1}</td></tr>", result.Description, result.Status.DisplayName));
                }
                _iowriter.WriteLine("</tbody");
                _iowriter.WriteLine("</table>");
                _iowriter.WriteLine("</div>");
            }

            _iowriter.WriteLine("</body>");
            _iowriter.WriteLine("</html>");
            _iowriter.Close();
        }
    }
}